<?php
if (file_exists('./function/install.lock')) header("Location:./login.php");
require_once './component/head.php';
require_once './component/drawer.php';
?>
<script type="text/javascript">
    window.onload = function () {
        InstallOnload();
    };
</script>

<div class="mdui-container" id="install_page">
    <?php
    if (isset($_GET['step']) && $_GET['step'] == 2) {
        require_once './function/conn.php';
        $php_ver_code = phpversion();
        $php_ver = $php_ver_code >= 7.3 ? 'Yes' : 'No';
        $mysqli = function_exists("mysqli_connect") ? 'Mysqli' : '未开启';
        $mysqli_ver = function_exists("mysqli_connect") ? 'Yes' : 'No';
        $db_ver_code = GetDbVersion()[0];
        $db_veri = explode("-", $db_ver_code)[0] > 5.4 ? 'Yes' : 'No';
        //文件完整性校验
        if (file_exists('./component/drawer.php') && file_exists('./component/footer.php') &&
            file_exists('./component/head.php') && file_exists('./function/conn.php') &&
            file_exists('./function/drxs.sql') && file_exists('./function/drxs_data.sql') &&
            file_exists('./function/function.php') && file_exists('./function/install_db.php') &&
            file_exists('./center.php') && file_exists('./course.php') &&
            file_exists('./docs.php') && file_exists('./index.php') &&
            file_exists('./login.php') && file_exists('./management.php') &&
            file_exists('./notice.php') && file_exists('./score.php') &&
            file_exists('./settings.php')) {
            $file_check = '文件完整';
            $file_ver = 'Yes';
        } else {
            $file_check = '文件缺失';
            $file_ver = 'No';
        }
        if (($file_ver == 'Yes')) {
            $init_data = '';
        } else {
            $init_data = 'disabled';
        }  //决定导入示例数据按钮是否被金庸
        if (($php_ver == 'Yes') && ($mysqli_ver == 'Yes') && ($db_veri == 'Yes') && ($file_ver == 'Yes')) {
            $next2 = '';
        } else {
            $next2 = 'disabled';
        }  //决定下一步按钮是否被禁用
        ?>
        <div class="mdui-card mdui-center" id="install_step2">
            <div class="mdui-card-content">
                <div class="mdui-typo">
                    <h3>运行环境检查<small>&nbsp;&nbsp;<label id="install_db_data_info"></label></small></h3>
                </div>
                <table class="mdui-table drxs-radius mdui-table-hoverable" style="margin-top: 10px">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>运行要求</th>
                        <th>当前环境</th>
                        <th>是否满足</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>1</td>
                        <td>PHP>=7.3</td>
                        <td>PHP: <?php echo $php_ver_code; ?></td>
                        <td id="install_php_ver"><?php echo $php_ver; ?></td>
                    </tr>
                    <tr>
                        <td>2</td>
                        <td>Mysqli插件</td>
                        <td><?php echo $mysqli; ?></td>
                        <td id="install_mysqli"><?php echo $mysqli_ver; ?></td>
                    </tr>
                    <tr>
                        <td>3</td>
                        <td>Mysql版本</td>
                        <td><?php echo $db_ver_code; ?></td>
                        <td id="install_db_ver"><?php echo $db_veri; ?></td>
                    </tr>
                    <tr>
                        <td>4</td>
                        <td>文件完整性校验</td>
                        <td><?php echo $file_check; ?></td>
                        <td id="install_db_ver"><?php echo $file_ver; ?></td>
                    </tr>
                    </tbody>
                </table>
                <div class="mdui-card-actions">
                    <button class="mdui-btn mdui-ripple mdui-float-left" id="install_db_data" <?php echo $init_data; ?>>
                        导入示例数据
                    </button>
                    <button class="mdui-btn mdui-ripple mdui-float-right" id="install_next_2" <?php echo $next2; ?>>下一步
                        &gt;
                    </button>
                    <button class="mdui-btn mdui-ripple mdui-float-right" id="install_pre_2">&lt; 上一步</button>
                </div>
            </div>
        </div>
    <?php } elseif (isset($_GET['complete'])) {
        require_once './function/conn.php';
        $user_num = mysqli_num_rows(GetAllInfo('drxs_user'));
        if ($user_num == 0) {
            global $conn;
            $pwd = password_hash('admin', PASSWORD_ARGON2ID);
            $date = date('Y-m-d');
            $sql = "insert into drxs_user(Id, Password, Name, Email, Date, Type) VALUES ('1','$pwd','管理员','admin','$date','管理员')";
            $sql_ustyle = "INSERT into drxs_ustyle(Uid, ThemeColor, ThemeAccentColor, TableColor1, TableColor2, TableColor3)VALUES('1','mdui-theme-primary-white','mdui-theme-accent-blue','mdui-color-white','mdui-color-white','mdui-color-white')";
            mysqli_query($conn, $sql);
            mysqli_query($conn, $sql_ustyle);
        }
        $installed = fopen("./function/install.lock", "w");
        fwrite($installed, '学生信息管理系统安装成功！');
        fclose($installed);
        ?>
        <div class="mdui-card mdui-center">
            <div class="mdui-card-primary">
                <div class="mdui-card-primary-title">安装成功！</div>
            </div>
            <div class="mdui-card-content">
                <div class="mdui-typo-"><h4>恭喜，学生信息管理系统已安装成功，请记录下方管理员账号密码后登录访问！</h4>
                    <p><u>管理员账号：admin</u></p>
                    <p><u>管理员密码：admin</u></p>
                    <p>可登录后修改！</p>
                </div>
                <div class="mdui-divider"></div>
            </div>
            <div class="mdui-card-actions">
                <button onclick="location.href='login.php'" class="mdui-btn mdui-ripple mdui-float-right">登录</button>
                <button id="delete_install_files" class="mdui-btn mdui-ripple mdui-float-right">删除安装文件</button>
                <button class="mdui-btn mdui-ripple mdui-float-right" id="install_pre_3">&lt; 上一步</button>
            </div>
        </div>
    <?php } else { ?>
        <div class="mdui-card mdui-center" id="install_step1">
            <div class="mdui-card-content">
                <div class="mdui-typo">
                    <h3>数据库</h3>
                    <div class="mdui-divider"></div>
                </div>
                <div class="mdui-textfield ">
                    <label class="mdui-textfield-label">数据库地址</label>
                    <input class="mdui-textfield-input" id="install_db_host" type="text" required/>
                </div>
                <div class="mdui-textfield">
                    <label class="mdui-textfield-label">数据库名称</label>
                    <input class="mdui-textfield-input" id="install_db_name" type="text" required/>
                </div>
                <div class="mdui-textfield">
                    <label class="mdui-textfield-label">数据库用户名</label>
                    <input class="mdui-textfield-input" id="install_db_user" type="text" required/>
                </div>
                <div class="mdui-textfield">
                    <label class="mdui-textfield-label" for="install_db_pwd">数据库用户密码</label>
                    <input class="mdui-textfield-input" id="install_db_pwd" type="password"/>
                </div>
                <br>
                <div class="mdui-row">
                    <button class="mdui-btn mdui-ripple mdui-float-left" id="install_db_conn">测试连接数据库</button>

                </div>
                <div class="mdui-card-actions">
                    <label id="install_db_info" class="mdui-float-left"></label>
                    <button class="mdui-btn mdui-ripple mdui-float-right" id="install_next_1"
                            mdui-tooltip="{content: '单击下一步将创建数据表'}" disabled>下一步>
                    </button>
                </div>
            </div>
        </div>
    <?php }
    require_once './component/footer.php';
    ?>
